package com.zhia.demo.web;


import com.zhia.demo.entity.NovelEntity;
import com.zhia.demo.service.NovelService;
import com.zhia.demo.vo.AddRequestVo;
import com.zhia.demo.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import org.thymeleaf.util.StringUtils;

/**
 * @author DONG
 * @create 2023/10/24 17:52
 */
@Controller
public class PageController {
    @Autowired
    NovelService novelService;

    @GetMapping("/search.html")
    public String searchPage() {
        return "index";
    }

    @GetMapping("/browse.html/{id}")
    public String browsePage(@PathVariable("id") String id, Model model) {
        model.addAttribute("id", id);
        return "browse";
    }

    @GetMapping("/bookcase.html/{id}")
    public String bookcasePage(@PathVariable("id") Integer id, Model model) {
        model.addAttribute("id", id);
        return "bookcase";
    }

    @GetMapping("/add.html")
    public String addPage() {
        return "addBook";
    }

    @GetMapping("/update.html/{id}")
    public String updatePage(@PathVariable("id") String id, Model model) {
        NovelEntity novel = novelService.getById(id);
        String intro = novel.getIntro();
        novel.setIntro(intro.replace("\n", ""));
        model.addAttribute("novel", novel);
        System.out.println(novel);
        return "addBook";
    }

    @PostMapping("/book/update/{id}")
    public String updateBook(@PathVariable("id") String id, AddRequestVo requestVo, RedirectAttributes redirectAttributes) {
        ResultVo resultVo = novelService.updateBook(id, requestVo);
        redirectAttributes.addFlashAttribute("novel", resultVo.getData());
        if(resultVo.getCode() == 0L) {
            redirectAttributes.addFlashAttribute("msg", "修改成功");
        }else {
            redirectAttributes.addFlashAttribute("msg", resultVo.getMessage());
        }
        return "redirect:/add.html";
    }

    @GetMapping("/book/delete/{id}")
    public String deleteBook(@PathVariable("id") String id, @CookieValue(name = "USERSESSION", defaultValue = "") String cookie, RedirectAttributes redirectAttributes) {
        novelService.removeById(id);
        if (!StringUtils.isEmpty(cookie)) {
            int userId = Integer.parseInt(cookie);
            redirectAttributes.addFlashAttribute("msg", "删除成功");
            return "redirect:/bookcase.html/" + userId;
        }else {
            return "redirect:/";
        }
    }


    @PostMapping("/book/add")
    public String addBook(AddRequestVo requestVo, @CookieValue(name = "USERSESSION", defaultValue = "") String cookie, RedirectAttributes redirectAttributes) {
        if (!StringUtils.isEmpty(cookie)) {
            int userId = Integer.parseInt(cookie);
            ResultVo resultVo = novelService.saveBook(requestVo, userId);
            if (resultVo.getCode() == 0L) {
                if (StringUtils.isEmpty(resultVo.getMessage())) {
                    redirectAttributes.addFlashAttribute("msg", "添加成功");
                } else {
                    redirectAttributes.addFlashAttribute("msg", resultVo.getMessage());
                }
                return "redirect:/bookcase.html/" + userId;
            } else {
                redirectAttributes.addFlashAttribute("msg", resultVo.getMessage());
                return "redirect:/add.html";
            }
        } else {
            return "redirect:/";
        }

    }
}
